using System;
using System.IO;
using HuaXianQu.ProtoBuffEx.Runtime;
using UnityEditor.AssetImporters;
using UnityEngine;

namespace HuaXianQu.ProtoBuffEx.ToolEditor
{
    [ScriptedImporter(1, "protodat"), Serializable]
    [Icon("d_TextAsset Icon")]
    public class ProtoDataImporter : ScriptedImporter
    {

        public override void OnImportAsset(AssetImportContext ctx)
        {
 
            MessageSerializer mMessageSerializer = ScriptableObject.CreateInstance<MessageSerializer>();
            mMessageSerializer.hideFlags = HideFlags.None;
            mMessageSerializer.ProtoType = Path.GetFileNameWithoutExtension(ctx.assetPath);
            // ReSharper disable once AssignNullToNotNullAttribute
            mMessageSerializer.ProtoData = File.ReadAllBytes(ctx.assetPath);

            ctx.AddObjectToAsset(mMessageSerializer.ProtoType, mMessageSerializer);
            ctx.SetMainObject(mMessageSerializer);
        }
    }
}